package com.cluster.controller;

import com.cluster.pojo.User;
import com.cluster.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    /**
     * 登录页
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin() {
        logger.info("===========跳转登录页面============");
        return "login";
    }

    /**
     * 登录请求
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("loginSystem")
    public String loginSystem(String username, String password, HttpSession session) {
        User user = userService.findByUsername(username);
        if (null == user) {
            logger.info("根据username:{}, 查询不到用户!", username);
            return "redirect:/login/toLogin";
        }
        if (!user.getPassword().equals(password)) {
            logger.info("密码password:{}, 错误!", password);
            return "redirect:/login/toLogin";
        }
        session.setAttribute("username",username + System.currentTimeMillis());
        logger.info("用户:{}, 成功登陆! 跳转到请求结果页!", username);
        return "redirect:/demo/result";
    }
}
